define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'weather_warning/index' + location.search,
                    add_url: 'weather_warning/add',
                    edit_url: 'weather_warning/edit',
                    del_url: 'weather_warning/del',
                    multi_url: 'weather_warning/multi',
                    import_url: 'weather_warning/import',
                    table: 'weather_warning',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'type', title: __('Type'), operate: 'LIKE'},
                        {field: 'level', title: __('Level'), operate: 'LIKE'},
                        {field: 'company', title: __('Company'), operate: 'LIKE'},
                        {field: 'time', title: __('Time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: {0: '未发送', '1': '已发送'},
                            formatter: function (val, obj) {
                                return obj.status == 1 ? "<span style='color:red'>已发送</span>" : '未发送';
                            }
                        },
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'push',
                                    title: '推送',
                                    classname: 'btn btn-xs btn-info btn-ajax btn-dialog',
                                    icon: 'fa fa-send',
                                    url: 'weather_warning/send?id={id}',
                                    confirm: '确认推送吗？',
                                    success:function(data, ret) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                    error:function(err){
                                        console.log(err);
                                    },
                                    hidden: function( row ){
                                        if(row.status==1){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    }
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});